<template>
    <view v-for="(item,index) in currentValue" :key="index" class='rx-RelateProcess'>
        <view class="rx-top">
            <view class="rx-icon-le">
                <view class="rx-icon_liucheng">
                    <my-icon type="icon-Vue3jpaas-icon_liucheng"></my-icon>
                </view>
            </view>
            <view style="flex: 1;overflow: hidden">
                <view class="rx-subject">{{ item.subject }}</view>
                <view class="rx-order"><span>{{ i18n('orderNum', '业务单号：') }}</span><span class="rx-order-name">{{ item.billNo }}</span></view>
                <view class="rx-order"><span>{{ i18n('addBy', '添加人：') }}</span><span class="rx-order-name">{{ item.addUser.userName }}</span></view>
            </view>
        </view>
        <i v-if="!readOnly" @click="bpmDelete(index)" class="iconfont rx-relate-delete icon-icon-test26"></i>
    </view>
    <view v-if="!readOnly" @click="setProcess" class="rx-relate-process-add">
        <i class="iconfont icon-jia"></i>{{ i18n('addAssociatedBpm', '添加关联流程') }}
    </view>
    <view class="rx-bpm" v-if="bpmShow">
        <BpmMyEvents @cancel="cancel" @confirm="confirm" :conf="conf" ref="RxBpm"></BpmMyEvents>
    </view>
</template>

<script>
/**
 * 关联流程
 */
import MForm from "../js/MForm";
import {FormApi} from "@/api/FormApi.js";
import BpmMyEvents from "./BpmMyEvents.vue"
import dayjs from "dayjs";

export default {
    name: "MRelateProcess",
    components: {BpmMyEvents},
    mixins: [MForm],
    data() {
        return {
            classifyQueryParam: {}, //分类的传参数
            ProcessList: [],
            bpmShow: false,
            currentValue: [],
            serverAddress: "",
            currentUser: {},
        }
    },
    computed: {
        conf() {
            return this.metadata.options;
        },
    },
    created() {
        this.getServerAddress();
    },
    mounted() {
        this.init(this.value);
    },
    methods: {
        i18n(name, text, key) {
            return this.$ti18(name, text, "formLang", key);
        },
        getTwoLetter(word) {
            if (!word) {
                return "";
            } else if (word.length < 2) {
                return word.toUpperCase();
            } else {
                return word.substr(0, 2).toUpperCase();
            }
        },
        init(val) {
            if (val && val != '') {
                this.currentValue = JSON.parse(val);
            }
        },
        // 删除
        bpmDelete(index) {
            this.currentValue.splice(index, 1)
        },
        getServerAddress() {
            FormApi.getServerAddress().then(res => {
                this.serverAddress = res.serverAddress;
                this.currentUser = res.currentUser;
            });
        },
        //添加流程
        setProcess() {
            this.bpmShow = true;
        },
        confirm(obj) {
            let date = dayjs().format('YYYY-MM-DD HH:mm:ss');
            if (this.conf.mode == 'default') {
                // 单选
                this.currentValue = [{
                    "subject": obj[0].subject,
                    "billNo": obj[0].billNo,
                    "instId": obj[0].instId,
                    "addUser": this.currentUser,
                    "addDate": date,
                    "isNew": true
                }];
            } else {
                // 多选
                for (let i = 0; i < obj.length; i++) {
                    this.currentValue.push({
                        "subject": obj[i].subject,
                        "billNo": obj[i].billNo,
                        "instId": obj[i].instId,
                        "addUser": this.currentUser,
                        "addDate": date,
                        "isNew": true
                    })
                }
            }
            this.bpmShow = false;
        },
        cancel() {
            this.bpmShow = false;
        }
    },
    watch: {
        currentValue: {
            handler: function (val) {
                this.setData(JSON.stringify(val))
            },
            deep: true
        }
    },
}
</script>

<style scoped>
.rx-bpm {
    width: 100%;
    height: 100%;
    text-align: left;
    position: fixed;
    top: 0px;
    left: 0;
    z-index: 99999;
    background-color: white;
}
.rx-RelateProcess {
    position: relative;
    margin: 1rem 0.5rem;
    border-bottom: 1px solid rgba(232, 232, 232, 1);
}
.rx-top {
    display: flex;
    overflow: hidden;
    margin: 0.5rem 0rem;
}
.rx-icon-le{
    background: rgba(239, 245, 255, 1);
    width: 3rem;
    height: 3rem;
    margin-right: 1rem;
    border-radius: 0.5rem;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
}
.rx-icon_liucheng{
    width: 2rem;
    height: 2rem;
    background: rgba(66, 133, 244, 1);
    border-radius: 0.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
}
.rx-icon_liucheng i{
    font-size: 1.8rem;
    color: white;
}
.rx-subject {
    font-size: 1rem;
    font-weight: bold;
    color: rgba(51, 51, 51, 1);
    margin-bottom: 0.5rem;
}
.rx-order{
    display: flex;
    line-height:1.5rem;
    align-items: center;
    color: rgba(0, 0, 0, 0.45);
    overflow: hidden;
}
.rx-order-name{
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
}
.rx-relate-delete{
    position: absolute;
    top: -1rem;
    font-size: 1.2rem;
    right: -0.5rem;
    color: rgba(255, 81, 81, 1);
}
.rx-relate-process-add {
    color: white;
    background: rgba(66, 133, 244, 1);
    text-align: center;
    line-height: 2rem;
    height: 2rem;
    border-radius: 0.25rem;
    margin-bottom: .8rem;
}

.rx-relate-process-add i {
    margin-right: .5rem;
    margin-top: .1rem;
}
</style>
